package com.business.lgh.naocs.naming.consistency;

import com.business.lgh.naocs.naming.health.HealthTask;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lgh
 * @version V1.0
 * @Package com.business.lgh.naocs.naming.health
 * @date 2020/6/25 7:59 下午
 */
@Slf4j
public class GlobalExecutorPools {

    static ScheduledExecutorService EXECUTOR;

    private static Map<String, ScheduledFuture> futureMap = new ConcurrentHashMap<>();

    /**
     * 实例化线程池
     */
    static {
        EXECUTOR= Executors.newScheduledThreadPool(5, new ThreadFactory() {

            AtomicInteger count=new AtomicInteger(0);
            private String THREAD_NAME="Global-";

            @Override
            public Thread newThread(Runnable r) {
                String name = THREAD_NAME + count.getAndIncrement();
                log.info(name);
                Thread thread=new Thread(r,name);
                thread.setDaemon(true);
                return thread;
            }
        });
    }



    /**
     * 立即提交任务
     * @param task
     */
    public static void submitNow(Runnable task){
        EXECUTOR.schedule(task,  5, TimeUnit.SECONDS);
    }


    /**
     * 立即提交任务
     * @param task
     */
    public static void submitDelay(Runnable task,int delay){
        EXECUTOR.schedule(task,  delay, TimeUnit.SECONDS);
    }
}
